<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <!--[if lt IE 9]>
    <script type="text/javascript" src="../../lib/html5shiv.js?rev=@@hash"></script>
    <script type="text/javascript" src="../../lib/respond.min.js?rev=@@hash"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="../../static/h-ui/css/H-ui.min.css"/>
    <link rel="stylesheet" type="text/css" href="../../static/h-ui.admin/css/H-ui.admin.css"/>
    <link rel="stylesheet" type="text/css" href="../../lib/Hui-iconfont/1.0.8/iconfont.css"/>
    <link rel="stylesheet" type="text/css" href="../../static/h-ui.admin/skin/blue/skin.css"/>
    <link rel="stylesheet" type="text/css" href="../../static/h-ui.admin/css/style.css"/>
    <link rel="stylesheet" type="text/css" href="../../ht-css/Project.css"/>
    <!--引入分页-->
    <link rel="stylesheet" href="../../lib/pagination/pagination.css">
    <!--[if IE 6]>
    <script type="text/javascript" src="../../lib/DD_belatedPNG_0.0.8a-min.js?rev=@@hash"></script>
    <script>DD_belatedPNG.fix('*');</script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="../../ht-css/style-q.css"/>
    <title>公司配置</title>
    <style>

    </style>
</head>
<body>
<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 权限管理 <span class="c-gray en">&gt;</span>  账号管理 <span class="c-gray en">&gt;</span> 数据配置 <span class="c-gray en">&gt;</span>公司配置
    <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="Hui-iconfont">&#xe68f;</i></a></nav>
<article class="page-container" method="post">
    <form class="ml-20 mr-20">
        <div class="bg-1 bk-gray">
            <div class="pl-10 lh-50 f-16">
              <!--  <strong>区域配置</strong>-->
            </div>
            <table class="table-hover table_li mb-10 table">
                <thead class="text-l">
                <tr>
                    <!-- <td class="col-4-1"><div class="row ml-10"><label class="form-label f-l w-75 text-r lh-26">区域：</label><div class="formControls f-l"><select id="ascription_lsit" class="select radius f-l ml-10" name="brandclass" size="1" style="width:226%; height: 27px;">
                        <script id="ascription_bob" type="text/x-dot-template">
                            <option  value="" selected>请选择区域</option>
                            {{if( it && it.length>0 ){ }}
                            {{ for(var i=0; i< it.length; i++) { }}
                            <option value="{{=it[i].areaId}}">{{=it[i].areaName}}</option>
                            {{ } }}
                            {{ }else{ }}
                            <option>没有数据</option>
                            {{ } }}
                        </script>
                    </select>
                    </div></div></td> -->
                    <td class="col-4-1"><div class="row ml-10"><label class="form-label f-l w-75 text-r lh-26">公司名称：</label><div class="formControls f-l"><input type="text" id="organizationName" class="input-text radius ml-10 w-180" placeholder="请输入公司名称">
                    </div></div></td>
                    <td class="col-4-2"><input type="button" onclick="companydd()" class="btn btn-secondary radius size-S f-l" value="搜&nbsp;&nbsp;索"></td>
                </tr>
                </thead>
            </table>
            <table class="table table-border table-bg table-sort" id="companydatatable">
                <thead>
                <tr class="text-c">
                    <th><input type="checkbox" onclick="stoprop()" value="spu_all" id="spu_all">全选</th>
                    <th>序号</th>
                    <!-- <th>所属区域</th> -->
                    <th>公司名称</th>
                </tr>
                </thead>
                <tbody class="shun">

                </tbody>
            </table>

            <div class="text-c pb-20 pt-10">
                <input id="sureBtn" type="button" onclick="addcompany()" class="btn btn-secondary radius size-S" value="确&nbsp;&nbsp;认">
                <input type="button" class="btn radius size-S ml-10" onclick="removeThise()" value="取&nbsp;&nbsp;消">
            </div>
        </div>
    </form>
</article>
<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="../../lib/jquery/1.9.1/jquery.min.js?rev=@@hash"></script>
<script type="text/javascript" src="../../lib/layer/2.4/layer.js?rev=@@hash"></script>
<script type="text/javascript" src="../../static/h-ui/js/H-ui.min.js?rev=@@hash"></script>
<script type="text/javascript" src="../../static/h-ui.admin/js/H-ui.admin.js?rev=@@hash"></script>
<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="../../lib/My97DatePicker/4.8/WdatePicker.js?rev=@@hash"></script>
<!--表单验证-->
<script type="text/javascript" src="../../lib/jquery.validation/1.14.0/jquery.validate.js?rev=@@hash"></script>
<script type="text/javascript" src="../../lib/jquery.validation/1.14.0/validate-methods.js?rev=@@hash"></script>
<script type="text/javascript" src="../../lib/jquery.validation/1.14.0/messages_zh.js?rev=@@hash"></script>
<script type="text/javascript" src="../../lib/datatables/1.10.0/jquery.dataTables.min.js?rev=@@hash"></script>
<script type="text/javascript" src="../../lib/laypage/1.2/laypage.js?rev=@@hash"></script>
<script src="../../ht-js/frame/doT.min.js?rev=@@hash"></script>
<script src="../../ht-js/public.js?rev=@@hash"></script>
<!--验证token-->
<script src="../../ht-js/verifyToken.js?rev=@@hash"></script>
<script src="../../ht-js/Project.js?rev=@@hash"></script>
<script src="../../lib/pagination/jquery.pagination.js?rev=@@hash"></script>
<script>
    function removeThise() {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }
    function stoprop() {
        var timer = setTimeout(function() {
            var num = $("#datatable").find("tbody").find("input[type=checkbox]:checked").length;
            setBtnDis(num, $("#productManage"));
            clearTimeout(timer);
        }, 200)

        window.event ? window.event.cancelBubble = true : e.stopPropagation();
    }
    function setBtnDis(num, pnode) { //如果选中项为空时，置灰相关按钮
        if(num <= 0) {
            pnode.find(".goods_edit").attr("disabled", "disabled").addClass("dis_btn");
        } else {
            pnode.find(".goods_edit").removeAttr("disabled").removeClass("dis_btn");
        }
    }
    var tk=""
    var objArrdd = [];
    var userid=sessionStorage.getItem("userids");
    //页面渲染
    company(1)
    function company(val) {
        tk=val
        var ascription_lsit=""
        if($("#ascription_lsit").val()==null){
            ascription_lsit=""
        }else{
            ascription_lsit=$("#ascription_lsit").val()
        }
       /* root.accessInter.getOrganizationList*/
        $.ajax({
            url:root.accessInter.getOrganizationList,
            contentType: "application/json;charset=UTF-8",
            cache: false,  //禁用缓存
            type: "POST",
            dataType:'json',
            data: JSON.stringify({
                "userId":userid,
                "area": ascription_lsit,
                "pageSize": 100000,
                "pageNum": 1,
                "organizationName": $("#organizationName").val()
            }),
            success: function (data) {
                companyTabls(data.data.data, 0);
                $('.M-box3').pagination({
                    pageCount: data.data.pageCount,
                    jump: true,
                    coping: true,
                    prevContent: '上页',
                    nextContent: '下页',
                    callback: function (api) {
                        /* root.accessInter.getOrganizationList*/
                        $.ajax({
                            url: root.accessInter.getOrganizationList,
                            contentType: "application/json;charset=UTF-8",
                            cache: false,  //禁用缓存
                            type: "POST",
                            dataType:'json',
                            data: JSON.stringify({
                                "userId":userid,
                                "area": ascription_lsit,
                                "pageSize": 100000,
                                "pageNum": api.getCurrent(),
                                "organizationName": $("#organizationName").val()
                            }),
                            success: function (result) {
                                companyTabls(result.data.data);
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                alert(XMLHttpRequest);
                            }
                        });
                    }
                });

            },
            error: function () {
                $.Huimodalalert('失败！',2000);
            }
        });
    }

    /*表格渲染*/
    var dataSource = new Array();
    //table的数据源
    var table;   //table对象
    dataSource = [];

    function companyTabls(data) {
        // console.log(data)
        dataSource.splice(0, dataSource.length);//清空数组
        $.each(data, function (i, field) {
            var rowData = new Array();
            rowData.push(field.companyName ? field.companyName :'');
            rowData.push(field.isCenterCompany?field.isCenterCompany:'');
            rowData.push(field.companyId ?field.companyId :'');
            dataSource.push(rowData);
        });
        console.log(dataSource)
        initDataTable(dataSource);
        
        function initDataTable(dataSource) {
            var dttable = $('#companydatatable').dataTable(); // 初始化dataTable，清空上一个值
            dttable.fnClearTable(); //清空一下table
            dttable.fnDestroy(); //还原初始化了的datatable
            $("#companydatatable").DataTable({
                 "fnDrawCallback" : function(){
                     this.api().column(1).nodes().each(function(cell,i){
                         cell.innerHTML = i+1;

                     });
                 },
                "autoWidth": false,
                "ordering": false,
                "searching": false,
                "paging": false,
                "retrieve": true,
                "destroy": false,
                "bAutoWidth": false,
                "bLengthChange": false,
                "aLengthMenu": [2],
                "oLanguage": {
                    "sLengthMenu": "每页显示 _MENU_ 条记录",
                    "sZeroRecords": "抱歉， 没有找到",
                    "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                    "sInfoEmpty": "没有数据",
                    "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "前一页",
                        "sNext": "后一页",
                        "sLast": "尾页"
                    },
                    "sZeroRecords": "没有检索到数据",
                    "sProcessing": "<img src='./loading.gif' />",
                },
                data: dataSource,
                columnDefs: [
                    {
                        "visible": true,
                        "targets": 0
                    },
                    {
                        "render": function(data, type, row, meta) {
                            if(row[1]==0){
                                console.log('1' + row[1])
                                var val = '<span><td><input    type="checkbox" id="' + row[2] + '" name="regional" value = "' + row[0] + '"></td></span>';
                                return val;
                            }else {
                                console.log('0' + row[1])
                                var val = '<span><td><input   checked ="checked"  type="checkbox" name="regional" id="' + row[2] + '" value = "' + row[0] + '"></td></span>';
                                return val;
                            }

                        },
                        "targets": 0
                    },
                    // {
                    //     "render": function(data, type, row, meta) {
                    //         var val = row[0] ;
                    //         return 'val';
                    //     },
                    //     "targets": 2
                    // },
                    {
                        "render": function(data, type, row, meta) {
                            var val = row[0] ;
                            return val;
                        },
                        "targets": 2
                    },


                ],
                "fnInitComplete": function(oSettings, json) {
                    setBtnDis(0, $("#productManage"));
                    $("#datatable").on("change", '.regional', function() {
                        var num = $(".shun").find("input[type=checkbox]:checked").length;
                        setBtnDis(num, $("#productManage"));
                    });

                }
            });
        };
    }
    function company22(val) {
        tk=val
        var ascription_lsit=""
        if($("#ascription_lsit").val()==null){
            ascription_lsit=""
        }else{
            ascription_lsit=$("#ascription_lsit").val()
        }
        /* root.accessInter.getOrganizationList*/
        $.ajax({
            url:root.accessInter.getOrganizationList,
            contentType: "application/json;charset=UTF-8",
            cache: false,  //禁用缓存
            type: "POST",
            dataType:'json',
            data: JSON.stringify({
                "userId":userid,
                "area": ascription_lsit,
                "pageSize": 100000,
                "pageNum": 1,
                "organizationName": $("#organizationName").val()
            }),
            success: function (data) {
                companyTabls22(data.data.data, 0);
                $('.M-box3').pagination({
                    pageCount: data.data.pageCount,
                    jump: true,
                    coping: true,
                    prevContent: '上页',
                    nextContent: '下页',
                    callback: function (api) {
                        /* root.accessInter.getOrganizationList*/
                        $.ajax({
                            url: root.accessInter.getOrganizationList,
                            contentType: "application/json;charset=UTF-8",
                            cache: false,  //禁用缓存
                            type: "POST",
                            dataType:'json',
                            data: JSON.stringify({
                                "userId":userid,
                                "area": ascription_lsit,
                                "pageSize": 100000,
                                "pageNum": api.getCurrent(),
                                "organizationName": $("#organizationName").val()
                            }),
                            success: function (result) {
                                companyTabls22(result.data.data);
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                alert(XMLHttpRequest);
                            }
                        });
                    }
                });

            },
            
            error: function () {
                $.Huimodalalert('失败！',2000);
            }
        });
    }

    /*表格渲染*/
    var dataSource = new Array();
    //table的数据源
    var table;   //table对象
    dataSource = [];

    function companyTabls22(data) {
        dataSource.splice(0, dataSource.length);//清空数组
        $.each(data, function (i, field) {
            var rowData = new Array();
            // rowData.push(field.areaName ? field.areaName :'');
            rowData.push(field.companyName ? field.companyName :'');
            rowData.push(field.isCenterCompany?field.isCenterCompany:'');
            rowData.push(field.companyId ?field.companyId :'');
            // rowData.push(field.area ? field.area : '');
            // rowData.push(field.companyName ? field.companyName : '');
            
            dataSource.push(rowData);            

        });
        console.log(dataSource)
        initDataTable(dataSource);

        function initDataTable(dataSource) {
            var dttable = $('#companydatatable').dataTable(); // 初始化dataTable，清空上一个值
            dttable.fnClearTable(); //清空一下table
            dttable.fnDestroy(); //还原初始化了的datatable
            $("#companydatatable").DataTable({
                "fnDrawCallback" : function(){
                    this.api().column(1).nodes().each(function(cell,i){
                        cell.innerHTML = i+1;

                    });
                },
                "autoWidth": false,
                "ordering": false,
                "searching": false,
                "paging": false,
                "retrieve": true,
                "destroy": false,
                "bAutoWidth": false,
                "bLengthChange": false,
                "aLengthMenu": [2],
                "oLanguage": {
                    "sLengthMenu": "每页显示 _MENU_ 条记录",
                    "sZeroRecords": "抱歉， 没有找到",
                    "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                    "sInfoEmpty": "没有数据",
                    "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "前一页",
                        "sNext": "后一页",
                        "sLast": "尾页"
                    },
                    "sZeroRecords": "没有检索到数据",
                    "sProcessing": "<img src='./loading.gif' />",
                },
                data: dataSource,
                columnDefs: [
                    {
                        "visible": true,
                        "targets": 0
                    },
                    {
                        "render": function(data, type, row, meta) {
                                var val = '<span><td><input    type="checkbox" id="' + row[2] + '" name="regional" value = "' + row[0] + '"></td></span>';
                                return val;

                        },
                        "targets": 0
                    },
                    // {
                    //     "render": function(data, type, row, meta) {
                    //         var val = row[0] ;
                    //         return val;
                    //     },
                    //     "targets": 2
                    // },
                    {
                        "render": function(data, type, row, meta) {
                            var val = row[0] ;
                            return val;
                        },
                        "targets": 3
                    },


                ],
                "fnInitComplete": function(oSettings, json) {
                    setBtnDis(0, $("#productManage"));
                    $("#datatable").on("change", '.regional', function() {
                        var num = $(".shun").find("input[type=checkbox]:checked").length;
                        setBtnDis(num, $("#productManage"));
                    });

                }
            });
        };
    }

    ascription()
    function ascription() {
        /*root.accessInter.getAreaList*/
        $.ajax({
            url: root.accessInter.getAreaList,
            type: "get",
            dataType:'json',
            data: {
            },
            success: function (data) {
                if(data.errorCode==0){
                    var dataInter=data.data;
                    var interText = doT.template($("#ascription_bob").text());
                    $("#ascription_lsit").html(interText(dataInter));
                }

            },
        });
    }
    function companydd() {

        var val=$('input:checkbox[name="regional"]:checked').val();
        if(val!=undefined){ //是否被选中

            var curInd = layer.confirm('<span id="status" style="width:200px;height:100px;resize: none;" maxlength="200" >是否保存当前选中的数据？</span>', {title:'提示',btn: ['保存', '不保存']}, function(index){

                finish1()
                layer.close(curInd);
            },function(){
                company22(1)

            });


        }else {
            company(1)
        }






    }
    function finish1() {
        var curObj={}
        var regional=document.getElementsByName("regional");
        for(var  i=0;i<regional.length;i++){
            if(regional[i].checked==true) {
                var vals = regional[i].value
                var vald = vals.split(",")
                curObj = {
                    "organizationId": regional[i].id,
                    "organizationName":regional[i].value
                }
                objArrdd.push(curObj)

            }
        }
        for (var i = 0; i < objArrdd.length; i++) {
            if (i < objArrdd.length -1){

                for (var j = i+1; j < objArrdd.length; j++) {
                    if (objArrdd[i].organizationId == objArrdd[j].organizationId) {
                        objArrdd.splice(j,1);
                    }
                }
            }


        }
        company(2)

    }


    //保存
    function addcompany() {
        var val=$('input:checkbox[name="regional"]:checked').val();
        if(tk==1){
            if(val==undefined){ //是否被选中

                $.Huimodalalert('请选择公司！',2000);
                return false;

            }
        }

        var curObj={}
        var regional=document.getElementsByName("regional");
        for(var  i=0;i<regional.length;i++){
            if(regional[i].checked==true) {
                var vals=regional[i].value
                var vald=vals.split(",")
                curObj = {
                    "organizationId": regional[i].id,
                    "organizationName":regional[i].value
                }
                objArrdd.push(curObj)

            }
        }

        var inputss=""
        for (var i = 0; i < objArrdd.length; i++) {
            inputss=objArrdd[i].organizationName
            if (i < objArrdd.length -1){
                for (var j = i+1; j < objArrdd.length; j++) {
                    if (objArrdd[i].organizationId == objArrdd[j].organizationId) {
                        objArrdd.splice(j,1);
                    }
                }
            }


        }

        for (var i = 0; i < inputss.length; i++) {
            if(inputss[i].checked==false){
                for (var k = 0; k < objArrdd.length; k++) {
                    if (objArrdd[k].organizationId == inputss[i].id ) {
                        objArrdd.splice(k,1);
                    }
                }

            }
        }


       /* root.accessInter.saveOrganizationConfig*/
        $.ajax({
            url: root.accessInter.saveOrganizationConfig,
            contentType: "application/json;charset=UTF-8",
            cache: false,  //禁用缓存
            type: "POST",
            dataType:'json',
            data: JSON.stringify({
                "userId":userid,
                "configInfos":objArrdd
            }),
            success: function (data) {
                if(data.errorCode==0){
                    $.Huimodalalert('配置成功！',2000);
                    setTimeout( function () {
                        var index =  parent.location.reload();
                        parent.layer.close(index)
                    },1000);
                }
            },
        });
    }
/*//父级框选中
    fk()
    function fk(){
        if($(".regional:checked").length==$(".regional").length){
            $("#spu_all").prop("checked",true);
        }else{
            $("#spu_all").prop("checked",false);
        }
    }*/

</script>
</body>
</html>